//
//  TalkTableCell.m
//  Shengzhuli
//
//  Created by subook on 2017/8/26.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "TalkTableCell.h"
#import "TimeChangeTool.h"
#import "UIImageView+WebCache.h"
#import "PickUpCommentModel.h"

@interface TalkTableCell () {
    
    NSMutableArray *_imageViewsArr;
    UIImageView *_headImageView;//头像
    UILabel *_nickNameLabel;//昵称
    UILabel *_timeAgoLabel;//发布时间
    UILabel *_themeLabel;//主题
    UILabel *_contentLabel;//内容
    UIView *_bottomview;//用来放评论和赞
    
    UIView *_imageBottomView;
    UIView *_lineView;
    
    NSMutableArray *_commentLabelsArr;
    UIView *_commentView;
    //UIButton *_lookMoreBtn;
}

@end

@implementation TalkTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _headImageView = [[UIImageView alloc] init];
        _headImageView.frame = CGRectMake(15, 15, 35, 35);
        _headImageView.layer.cornerRadius = 35/2.0;
        _headImageView.clipsToBounds = YES;
        [self.contentView addSubview:_headImageView];
        
        _nickNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(_headImageView.right+10, 15, ScreenW-(_headImageView.right+10)-15, 15)];
        _nickNameLabel.textColor = RGBA(46, 135, 192, 1);
        _nickNameLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_nickNameLabel];
        
        _timeAgoLabel = [[UILabel alloc] initWithFrame:CGRectMake(_headImageView.right+10, _nickNameLabel.bottom+3, ScreenW-(_headImageView.right+10)-15, 12)];
        _timeAgoLabel.textColor = [UIColor grayColor];
        _timeAgoLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:_timeAgoLabel];
        
        _themeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, _headImageView.bottom+10, ScreenW-30, 16)];
        _themeLabel.font = [UIFont systemFontOfSize:16];
        _themeLabel.numberOfLines = 0;
        [self.contentView addSubview:_themeLabel];
        
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, _themeLabel.bottom+10, ScreenW-30, 60)];
        _contentLabel.textColor = [UIColor grayColor];
        _contentLabel.font = [UIFont systemFontOfSize:14];
        _contentLabel.numberOfLines = 0;
        [self.contentView addSubview:_contentLabel];
        
        _imageViewsArr = [NSMutableArray array];
        //三张图片
        CGFloat imageWidth = (ScreenW-30-5*2)/3;
        CGFloat space = 5;
        
        _imageBottomView = [[UIView alloc] initWithFrame:CGRectMake(0, _contentLabel.bottom+10, ScreenW, imageWidth)];
        [self.contentView addSubview:_imageBottomView];
        
        for (int i=0; i<3; i++) {
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:(CGRectMake(15+i*(imageWidth+space), 0, imageWidth, imageWidth))];
            imgView.userInteractionEnabled = YES;
            imgView.contentMode = UIViewContentModeScaleAspectFill;
            imgView.clipsToBounds = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTap:)];
            [imgView addGestureRecognizer:tap];
            [_imageBottomView addSubview:imgView];
            [_imageViewsArr addObject:imgView];
        };
        
        _bottomview = [[UIView alloc] initWithFrame:(CGRectMake(ScreenW-90, _imageBottomView.bottom+10, 80, 20))];
        [self.contentView addSubview:_bottomview];
        _zanButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        ;
        _zanButton.frame = CGRectMake(0, 0, 20, 20);
        [_zanButton setImage:GetImage(@"点赞") forState:(UIControlStateNormal)];
        [_zanButton setImage:GetImage(@"点赞selected") forState:(UIControlStateSelected)];
        [_zanButton addTarget:self action:@selector(zanButtonclick) forControlEvents:(UIControlEventTouchUpInside)];
        [_bottomview addSubview:_zanButton];
        
        _zanNumLabel = [[UILabel alloc] initWithFrame:(CGRectMake(20, 0, 20, 20))];
        _zanNumLabel.font = [UIFont systemFontOfSize:12];
        _zanNumLabel.textColor = [UIColor grayColor];
        [_bottomview addSubview:_zanNumLabel];
        
        //评论
        _commentButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _commentButton.frame = CGRectMake(40, 0, 20, 20);
        [_commentButton setImage:GetImage(@"评论") forState:(UIControlStateNormal)];
        [_commentButton addTarget:self action:@selector(pingBUttonClick) forControlEvents:(UIControlEventTouchUpInside)];
        [_bottomview addSubview:_commentButton];
        
        _pingNumLabel = [[UILabel alloc] initWithFrame:(CGRectMake(60, 0, 30, 20))];
        _pingNumLabel.textColor = [UIColor grayColor];
        _pingNumLabel.font = [UIFont systemFontOfSize:12];
        [_bottomview addSubview:_pingNumLabel];
        
        
        _commentLabelsArr = [NSMutableArray array];
        _commentView = [[UIView alloc] initWithFrame:CGRectMake(15, _bottomview.bottom+10, ScreenW-30, 15*3+10)];
        _commentView.backgroundColor = RGBA(245, 245, 245, 1);
        [self.contentView addSubview:_commentView];
        
        for (int i=0; i<3; i++) {
            UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(10, i*(15+5), ScreenW-30-20, 15))];
            label.numberOfLines = 0;
            label.font = [UIFont systemFontOfSize:14];
            label.textColor = [UIColor grayColor];
            [_commentView addSubview:label];
            [_commentLabelsArr addObject:label];
        };
        
        _lookMoreBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _lookMoreBtn.frame = (CGRectMake(10, 0, 75 , 15));
        [_lookMoreBtn setTitleColor:RGBA(46, 135, 192, 1) forState:(UIControlStateNormal)];
        [_lookMoreBtn setTitle:@"查看更多>>" forState:(UIControlStateNormal)];
        _lookMoreBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_lookMoreBtn addTarget:self action:@selector(lookMoreBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
        [_commentView addSubview:_lookMoreBtn];
        
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, _commentView.bottom+10, ScreenW, 0.5)];
        _lineView.backgroundColor = RGBA(230, 230, 230, 1);
        [self.contentView addSubview:_lineView];
        
    }
    return self;
}

- (void)imageTap:(UITapGestureRecognizer *)tap {
    
    UIImageView *imgView = (UIImageView *)tap.view;
    if (self.imageTapBlock) {
        if (imgView.image) {
            self.imageTapBlock(imgView.image);
        }
    }
}

- (void)lookMoreBtnAction {
    
    if (self.lookMoreBlock) {
        self.lookMoreBlock();
    }
}

- (void)zanButtonclick {
    
    if (self.zanButtonBlock) {
        self.zanButtonBlock();
    }
}
- (void)pingBUttonClick {
    NSLog(@"点击评论");
    if (self.pingButtonBlock) {
        self.pingButtonBlock();
    }
}

- (void)updateCellWithModel:(PickUpModel *)model {
    
    if ([model.isNotPraise boolValue]) {
        _zanButton.selected = YES;
    } else {
        _zanButton.selected = NO;
 
    }
    
    _imageBottomView.hidden = YES;
    for (UIImageView *imageView in _imageViewsArr) {
        imageView.hidden = YES;
    }
    _commentView.hidden = YES;
    _lookMoreBtn.hidden = YES;
    for (UILabel *label in _commentLabelsArr) {
        label.hidden = YES;
    }

    [_headImageView sd_setImageWithURL:[NSURL URLWithString:model.portraitUrl] placeholderImage:nil];
    _nickNameLabel.text = model.name;
    _themeLabel.text = model.ruleTitle;
    _timeAgoLabel.text = [TimeChangeTool updateTimeForRow:model.createDate];
    _contentLabel.text = model.ruleContent;
    _zanNumLabel.text = [NSString stringWithFormat:@"%@",model.praise];
    _pingNumLabel.text = [NSString stringWithFormat:@"%@",model.commentsNumber];
    
    CGFloat titleHeight = [self AutoSizeWithStr:model.ruleTitle textfont:[UIFont systemFontOfSize:16] size:CGSizeMake(ScreenW-30, MAXFLOAT)].height;
    
    CGFloat contentHeight = [self AutoSizeWithStr:model.ruleContent textfont:[UIFont systemFontOfSize:14] size:CGSizeMake(ScreenW-30, MAXFLOAT)].height;
    
    CGRect frame = _themeLabel.frame;
    frame.size.height = titleHeight;
    _themeLabel.frame = frame;
    
    frame = _contentLabel.frame;
    frame.origin.y = _themeLabel.bottom+10;
    frame.size.height = contentHeight;
    _contentLabel.frame = frame;
    
    CGFloat theBottom = _contentLabel.bottom;
    
    if (model.pictureUrlList.count>0) {
        for (int i=0; i<model.pictureUrlList.count; i++) {
            UIImageView *imageView = _imageViewsArr[i];
            imageView.hidden = NO;
            [imageView sd_setImageWithURL:[NSURL URLWithString:model.pictureUrlList[i]] placeholderImage:GetImage(@"beijngtu")];
        }
        _imageBottomView.hidden = NO;
        frame = _imageBottomView.frame;
        frame.origin.y = _contentLabel.bottom+10;
        _imageBottomView.frame = frame;
        
        theBottom = _imageBottomView.bottom;
    }
    
    frame = _bottomview.frame;
    frame.origin.y = theBottom+10;
    _bottomview.frame = frame;
    
    theBottom = _bottomview.bottom-5;
    
    if (model.ruleCommentsList.count>0) {
        
        CGFloat labelBottom = 0;
        
        if (model.ruleCommentsList.count > 3 && model.ruleCommentsList.count >_commentLabelsArr.count) {
            for (int i=0; i<model.ruleCommentsList.count-3; i++) {
                UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(10, 0, ScreenW-30-20, 15))];
                label.numberOfLines = 0;
                label.font = [UIFont systemFontOfSize:14];
                label.textColor = [UIColor grayColor];
                [_commentView addSubview:label];
                [_commentLabelsArr addObject:label];
            };
        }
        
        for (int i=0; i<model.ruleCommentsList.count; i++) {
            PickUpCommentModel *commentModel = model.ruleCommentsList[i];
            UILabel *commentLabel = _commentLabelsArr[i];
            commentLabel.hidden = NO;
            NSString *str1 = [NSString stringWithFormat:@"%@：%@",commentModel.nickName,commentModel.ruleCommentContent];
            NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc] initWithString:str1];
            [string1 addAttribute:NSForegroundColorAttributeName value:RGBA(46, 135, 192, 1) range:NSMakeRange(0,commentModel.nickName.length)];
            commentLabel.attributedText = string1;
            
            CGFloat commentHeight = [self AutoSizeWithStr:str1 textfont:[UIFont systemFontOfSize:14] size:CGSizeMake(ScreenW-50, MAXFLOAT)].height;
            
            CGRect labelFrame = commentLabel.frame;
            labelFrame.origin.y = labelBottom+5;
            labelFrame.size.height = commentHeight;
            commentLabel.frame = labelFrame;
            
            labelBottom = commentLabel.bottom;

        }
        
        CGFloat btnHeight = 0;
        if ([model.commentNumberStatus boolValue] && model.ruleCommentsList.count < 4) {

            _lookMoreBtn.hidden = NO;
            frame = _lookMoreBtn.frame;
            frame.origin.y = labelBottom+5;
            _lookMoreBtn.frame = frame;
            btnHeight = 20;
        }
        
        _commentView.hidden = NO;
        frame = _commentView.frame;
        frame.origin.y = theBottom+15;
        frame.size.height = labelBottom+5+btnHeight;
        _commentView.frame = frame;
        
        theBottom = _commentView.bottom;
    }

    frame = _lineView.frame;
    frame.origin.y = theBottom+15;
    _lineView.frame = frame;
}

- (CGSize)AutoSizeWithStr:(NSString *)str textfont:(UIFont *)textfont size:(CGSize)size {
    return [str boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine |
            NSStringDrawingUsesLineFragmentOrigin |
            NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:textfont} context:nil].size;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
